const BaseServervice = require('./baseService')

const AV = require('leanengine')

class Service extends BaseServervice {
  constructor() {
    super('Order')
  }

  async getDetail(ctx) {
    const {
      orderId,
      sessionToken
    } = ctx.params
    if (!sessionToken) {
      return {
        code: '603',
        msg: 'sessionToken不存在'
      }
    }
    if (!orderId) {
      return {
        code: 602,
        msg: 'orderId不存在'
      }
    }
    const query = new AV.Query('Order');
    try {
      let res = await query.get(orderId);
      let order = {
        ...res.attributes,
        orderId: res.id,
        ProductType: undefined,
        msg: undefined,
        finalPrice: undefined,
        scheduleId: undefined,
        address: undefined,
        createdAt: res.get('createdAt')
      }
      return {
        code: '666',
        msg: 'success',
        order
      }
    } catch (error) {
      return {
        code: error.code,
        msg: error.message
      }
    }



  }

  async getList(ctx) {
    const {
      sessionToken
    } = ctx.params;
    console.log('sessionToken', sessionToken);
    if (!sessionToken || sessionToken === 'undefined') {
      return {
        code: '603',
        msg: 'sessionToken不存在'
      }
    }
    const query = new AV.Query('Order')
    let list = await query.find();
    let orders = list.map(order => {
      return {
        orderId: order.id,
        price: order.get('price'),
        count: order.get('count'),
        originPrice: order.get('originPrice'),
        imgUrl: order.get('imgUrl'),
        status: order.get('status'),
        filmName: order.get('filmName'),
        watchtime: order.get('watchtime')
      }
    })
    return {
      code: '666',
      msg: 'success',
      orders
    }

  }
}

module.exports = new Service()